package com.cooksys.film_rental.bo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.FilmBo;
import com.cooksys.film_rental.dao.FilmDao;
import com.cooksys.film_rental.model.Actor;
import com.cooksys.film_rental.model.Category;
import com.cooksys.film_rental.model.Film;
import com.cooksys.film_rental.model.FilmActor;
import com.cooksys.film_rental.model.FilmCategory;
import com.cooksys.film_rental.model.FilmText;
import com.cooksys.film_rental.model.Inventory;
import com.cooksys.film_rental.model.Language;
import com.cooksys.film_rental.model.Store;

@Service("FilmBo")
public class FilmBoImpl implements FilmBo {

	@Autowired(required = true)
	FilmDao filmDao;

	@Override
	public List<Film> searchFilmsByPartialTitle(String partialTitle) {
		return this.filmDao.searchFilmsByPartialTitle(partialTitle);
	}

	@Override
	public long getNumCopiesAvailable(Film film) {
		return this.filmDao.getNumCopiesAvailable(film);
	}

	@Override
	public long getNumCopiesCheckedOut(Film film) {
		return this.filmDao.getNumCopiesCheckedOut(film);
	}

	@Override
	public Film getFilmByFilmId(Short filmId) {
		return this.filmDao.getFilmByFilmId(filmId);
	}

	@Override
	public Film getFilmByFilmId(String filmId) {

		try {

			Short filmIdAsShort = Short.valueOf(filmId);
			return this.getFilmByFilmId(filmIdAsShort);

		} catch (NumberFormatException e) {
			System.err
					.println("Could not parse Short value for filmId from string: "
							+ filmId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;
		}

	}

	@Override
	public long getNumCopiesAvailable(Short filmId) {
		Film film = this.getFilmByFilmId(filmId);
		return this.getNumCopiesAvailable(film);
	}

	@Override
	public long getNumCopiesAvailable(String filmId) {
		Film film = this.getFilmByFilmId(filmId);
		return this.getNumCopiesAvailable(film);
	}

	@Override
	public long getNumCopiesCheckedOut(Short filmId) {
		Film film = this.getFilmByFilmId(filmId);
		return this.getNumCopiesCheckedOut(film);
	}

	@Override
	public long getNumCopiesCheckedOut(String filmId) {
		Film film = this.getFilmByFilmId(filmId);
		return this.getNumCopiesCheckedOut(film);
	}

	@Override
	public FilmText getFilmTextByFilm(Film film) {
		return this.getFilmTextByFilmId(film.getFilmId());
	}

	@Override
	public FilmText getFilmTextByFilmId(Short filmId) {
		return this.filmDao.getFilmTextByFilmId(filmId);
	}

	@Override
	public FilmText getFilmTextByFilmId(String filmId) {
		try {

			Short filmIdAsShort = Short.valueOf(filmId);
			return this.getFilmTextByFilmId(filmIdAsShort);

		} catch (NumberFormatException e) {
			System.err
					.println("Could not parse Short value for filmId from string: "
							+ filmId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;
		}
	}

	@Override
	public List<Film> searchFilmsByLanguage(Language language) {
		return this.filmDao.searchFilmsByLanguage(language);
	}

	@Override
	public List<Film> searchFilmsByOriginalLanguage(Language originalLanguage) {
		return this.filmDao.searchFilmsByOriginalLanguage(originalLanguage);
	}

	@Override
	public Film getFilmByInventory(Inventory inventory) {
		return this.filmDao.getFilmByInventory(inventory);
	}

	@Override
	public Film getFilmByFilmActor(FilmActor filmActor) {
		return this.filmDao.getFilmByFilmActor(filmActor);
	}

	@Override
	public Film getFilmByFilmCategory(FilmCategory filmCategory) {
		return this.filmDao.getFilmByFilmCategory(filmCategory);
	}

	@Override
	public List<Film> searchFilmsByActor(Actor actor) {
		return this.filmDao.searchFilmsByActor(actor);
	}

	@Override
	public List<Film> searchFilmsByCategory(Category category) {
		return this.filmDao.searchFilmsByCategory(category);
	}

	@Override
	public List<String> getAllRatings() {
		return this.filmDao.getAllRatings();
	}

	@Override
	public short getMaxRuntime() {
		return this.filmDao.getMaxRuntime();
	}

	@Override
	public List<Film> advancedSearch(String partialTitle,
			Category categoryFilter, Actor actorFilter,
			Language languageFilter, Language originalLanguageFilter,
			String ratingFilter, String runtimeFilter) {

		return this.filmDao.advancedSearch(partialTitle, categoryFilter,
				actorFilter, languageFilter, originalLanguageFilter,
				ratingFilter, runtimeFilter);
	}

	@Override
	public boolean getFilmAvailabilityByStore(Film film, Store store) {
		return this.filmDao.getFilmAvailabilityByStore(film, store);
	}

}
